//
//  SDUserInformationModel.m
//
//  Created by   on 2018/11/8
//  Copyright (c) 2018 __MyCompanyName__. All rights reserved.
//

#import "SDUserInformationModel.h"


NSString *const kSDUserInformationModelDevice = @"device";
NSString *const kSDUserInformationModelHeadImage = @"headImage";
NSString *const kSDUserInformationModelAppVer = @"appVer";
NSString *const kSDUserInformationModelId = @"id";
NSString *const kSDUserInformationModelUserName = @"userName";
NSString *const kSDUserInformationModelAppId = @"appId";
NSString *const kSDUserInformationModelSkinType = @"skinType";
NSString *const kSDUserInformationModelSex = @"sex";
NSString *const kSDUserInformationModelNickName = @"nickName";
NSString *const kSDUserInformationModelToken = @"token";


@interface SDUserInformationModel ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SDUserInformationModel

@synthesize device = _device;
@synthesize headImage = _headImage;
@synthesize appVer = _appVer;
@synthesize id = _id;
@synthesize userName = _userName;
@synthesize appId = _appId;
@synthesize skinType = _skinType;
@synthesize sex = _sex;
@synthesize nickName = _nickName;
@synthesize token = _token;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.device = [self objectOrNilForKey:kSDUserInformationModelDevice fromDictionary:dict];
            self.headImage = [self objectOrNilForKey:kSDUserInformationModelHeadImage fromDictionary:dict];
            self.appVer = [self objectOrNilForKey:kSDUserInformationModelAppVer fromDictionary:dict];
            self.id = [[self objectOrNilForKey:kSDUserInformationModelId fromDictionary:dict] doubleValue];
            self.userName = [self objectOrNilForKey:kSDUserInformationModelUserName fromDictionary:dict];
            self.appId = [self objectOrNilForKey:kSDUserInformationModelAppId fromDictionary:dict];
            self.skinType = [[self objectOrNilForKey:kSDUserInformationModelSkinType fromDictionary:dict] doubleValue];
            self.sex = [[self objectOrNilForKey:kSDUserInformationModelSex fromDictionary:dict] doubleValue];
            self.nickName = [self objectOrNilForKey:kSDUserInformationModelNickName fromDictionary:dict];
            self.token = [self objectOrNilForKey:kSDUserInformationModelToken fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.device forKey:kSDUserInformationModelDevice];
    [mutableDict setValue:self.headImage forKey:kSDUserInformationModelHeadImage];
    [mutableDict setValue:self.appVer forKey:kSDUserInformationModelAppVer];
    [mutableDict setValue:[NSNumber numberWithDouble:self.id] forKey:kSDUserInformationModelId];
    [mutableDict setValue:self.userName forKey:kSDUserInformationModelUserName];
    [mutableDict setValue:self.appId forKey:kSDUserInformationModelAppId];
    [mutableDict setValue:[NSNumber numberWithDouble:self.skinType] forKey:kSDUserInformationModelSkinType];
    [mutableDict setValue:[NSNumber numberWithDouble:self.sex] forKey:kSDUserInformationModelSex];
    [mutableDict setValue:self.nickName forKey:kSDUserInformationModelNickName];
    [mutableDict setValue:self.token forKey:kSDUserInformationModelToken];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.device = [aDecoder decodeObjectForKey:kSDUserInformationModelDevice];
    self.headImage = [aDecoder decodeObjectForKey:kSDUserInformationModelHeadImage];
    self.appVer = [aDecoder decodeObjectForKey:kSDUserInformationModelAppVer];
    self.id = [aDecoder decodeDoubleForKey:kSDUserInformationModelId];
    self.userName = [aDecoder decodeObjectForKey:kSDUserInformationModelUserName];
    self.appId = [aDecoder decodeObjectForKey:kSDUserInformationModelAppId];
    self.skinType = [aDecoder decodeDoubleForKey:kSDUserInformationModelSkinType];
    self.sex = [aDecoder decodeDoubleForKey:kSDUserInformationModelSex];
    self.nickName = [aDecoder decodeObjectForKey:kSDUserInformationModelNickName];
    self.token = [aDecoder decodeObjectForKey:kSDUserInformationModelToken];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_device forKey:kSDUserInformationModelDevice];
    [aCoder encodeObject:_headImage forKey:kSDUserInformationModelHeadImage];
    [aCoder encodeObject:_appVer forKey:kSDUserInformationModelAppVer];
    [aCoder encodeDouble:_id forKey:kSDUserInformationModelId];
    [aCoder encodeObject:_userName forKey:kSDUserInformationModelUserName];
    [aCoder encodeObject:_appId forKey:kSDUserInformationModelAppId];
    [aCoder encodeDouble:_skinType forKey:kSDUserInformationModelSkinType];
    [aCoder encodeDouble:_sex forKey:kSDUserInformationModelSex];
    [aCoder encodeObject:_nickName forKey:kSDUserInformationModelNickName];
    [aCoder encodeObject:_token forKey:kSDUserInformationModelToken];
}

- (id)copyWithZone:(NSZone *)zone {
    SDUserInformationModel *copy = [[SDUserInformationModel alloc] init];
    
    
    
    if (copy) {

        copy.device = [self.device copyWithZone:zone];
        copy.headImage = [self.headImage copyWithZone:zone];
        copy.appVer = [self.appVer copyWithZone:zone];
        copy.id = self.id;
        copy.userName = [self.userName copyWithZone:zone];
        copy.appId = [self.appId copyWithZone:zone];
        copy.skinType = self.skinType;
        copy.sex = self.sex;
        copy.nickName = [self.nickName copyWithZone:zone];
        copy.token = [self.token copyWithZone:zone];
    }
    
    return copy;
}


@end
